package com.cdc.taskhandler;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicOperationDemo {
    static AtomicInteger count=new AtomicInteger(0);
    public static class AddThread implements Runnable{
        @Override
        public void run() {
            System.out.println("Thread ID- " +Thread.currentThread().getId() + " For "
                    + Thread.currentThread().getName());
            for(int k=0;k<1000;k++){
                count.incrementAndGet();
            }
        }
    }
    public static void AtomicIntShow(){
        System.out.println("AtomicIntShow() enter");
        ExecutorService threadpool =   Executors.newFixedThreadPool(10);

        for(int k=0;k<100;k++){
            System.out.println("========="+k);
            threadpool.submit(new Thread(new AddThread(),"Thread"+(k+1)));
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /* output
         * AtomicIntShow() enter
         * result of acumulated sum=100000
         * AtomicIntShow() exit
         */

        System.out.println("result of acumulated sum="+count);
        threadpool.shutdown();
        System.out.println("AtomicIntShow() exit");


        return ;

    }

    public static void main(String[] args) {
        AtomicOperationDemo.AtomicIntShow();
    }


}
